/**
 * Copyright 2014 lerry
 * 
 * All right reserved.
 * 
 * Dec 24, 2014
 */
package com.bac.bacplatform;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author lerry
 * 
 */
public class YeepayActivity extends BaseActivity {
	private static final String JS_METHOD_RETURN = "js_return_home";

	private WebView mWebView;
	private ProgressBar mPb;
	private TextView mCloseBt;

	@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yeepay_layout);

		mCloseBt = (TextView) findViewById(R.id.tv_close);
		mWebView = (WebView) findViewById(R.id.wb_yeepay);
		mPb = (ProgressBar) findViewById(R.id.pb);
		mPb.setMax(100);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setWebViewClient(new YeepayClient());
		mWebView.setWebChromeClient(new YeepayChromeClient());
		String paymentUrl = getIntent().getStringExtra("paymentUrl");
		mWebView.loadUrl(paymentUrl);
		// mWebView.loadUrl("file:///android_asset/test.html");

		mWebView.addJavascriptInterface(new JsReturnHomeObj(), JS_METHOD_RETURN);

		mCloseBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finishActivityOut();
			}
		});
	}

	@Override
	public void init() {

	}

	private class YeepayChromeClient extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			mPb.setProgress(newProgress);
			if (newProgress == 100) {
				mPb.setVisibility(View.GONE);
			}
			super.onProgressChanged(view, newProgress);
		}
	}

	private class YeepayClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);

			view.loadUrl("file:///android_asset/NetworkError.html");
		}

	}


	class JsReturnHomeObj {
		@JavascriptInterface
		public void returnHomeOnClick() {
			// 在该接口中添加JS可以调用的方法，用以对本地应用进行操作，本示例中的方法作用为返回充值页面。
			new Handler().post(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Intent intent = new Intent(YeepayActivity.this,
							MainActivity.class);
					Log.i("pay done", "pay yeepay pay done--");
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
//					finishActivityOut();
				}
			});
		}
	}
}
